//
//  LFTabBar.m
//  LoveFruit
//
//  Created by 张令林 on 16/9/25.
//  Copyright © 2016年 张令林. All rights reserved.
//

#import "LFTabBar.h"


@interface LFTabBar ()

@property (nonatomic,strong) UIButton *composeButton;


@end

@implementation LFTabBar

- (void)drawRect:(CGRect)rect {
    // Drawing code
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUpUI];
    }
    return self;
}

#pragma mark 初始化方法
- (void)setUpUI
{

    
}



#pragma mark 添加中间的button
- (void)addButton
{

}

#pragma mark - 重写布局
-(void)layoutSubviews
{
    [super layoutSubviews];
    
    //添加中间的按钮
    [self addSubview:self.composeButton];
    self.composeButton.center = CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 0.5);
    
    //布局下面的tabbar
    CGFloat itemW = [UIScreen mainScreen].bounds.size.width/(CGFloat)5;
//    NSInteger index = 0;
    
    for (int i = 0; i < self.subviews.count; i++)
    {
        UIView *subView = self.subviews[i];
        if ([subView isKindOfClass:NSClassFromString(@"UITabBarButton")])
        {
            //计算宽度和x的值
//            subView.x = itemW*(CGFloat)index;
//            subView.width = itemW;
//            NSLog(@"--------%zd---------",index);
//            index++;
//            if (index == 2)
//            {
//                index++;
//            }
            switch (i)
            {
                case 3:
                    subView.x = 0;
                    subView.width = itemW;
                    break;
                case 0:
                    subView.x = itemW;
                    subView.width = itemW;
                    break;
                case 1:
                    subView.x = itemW*3;
                    subView.width = itemW;
                    break;
                case 2:
                    subView.x = itemW*4;
                    subView.width = itemW;
                    break;
                default:
                    break;
            }
            
        }
    }

    
    
    
    
}
#pragma mark - 中间按钮的点击事件
- (void)composeButtonClick
{
    self.composeBtnBlock();
}


#pragma mark - 懒加载中间的控件
-(UIButton *)composeButton
{
    if (_composeButton == nil)
    {
        UIButton *button = [[UIButton alloc] init];
        [button setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
        [button setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
        [button sizeToFit];
        //添加点击事件
        [button addTarget:self action:@selector(composeButtonClick) forControlEvents:UIControlEventTouchUpInside];
        _composeButton = button;
    }
    return _composeButton;
}


@end
